{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "collapsed": true,
        "pycharm": {
          "is_executing": false,
          "name": "#%% md\n"
        }
      },
      "source": "## 数据处理示实例\n"
    },
    {
      "cell_type": "code",
      "execution_count": 102,
      "outputs": [
        {
          "data": {
            "text/plain": "   PassengerId  Survived  Pclass  \\\n0            1         0       3   \n1            2         1       1   \n2            3         1       3   \n3            4         1       1   \n4            5         0       3   \n\n                                                Name     Sex   Age  SibSp  \\\n0                            Braund, Mr. Owen Harris    male  22.0      1   \n1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n2                             Heikkinen, Miss. Laina  female  26.0      0   \n3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n4                           Allen, Mr. William Henry    male  35.0      0   \n\n   Parch            Ticket     Fare Cabin Embarked  \n0      0         A/5 21171   7.2500   NaN        S  \n1      0          PC 17599  71.2833   C85        C  \n2      0  STON/O2. 3101282   7.9250   NaN        S  \n3      0            113803  53.1000  C123        S  \n4      0            373450   8.0500   NaN        S  ",
            "text/html": "\u003cdiv\u003e\n\u003cstyle scoped\u003e\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n\u003c/style\u003e\n\u003ctable border\u003d\"1\" class\u003d\"dataframe\"\u003e\n  \u003cthead\u003e\n    \u003ctr style\u003d\"text-align: right;\"\u003e\n      \u003cth\u003e\u003c/th\u003e\n      \u003cth\u003ePassengerId\u003c/th\u003e\n      \u003cth\u003eSurvived\u003c/th\u003e\n      \u003cth\u003ePclass\u003c/th\u003e\n      \u003cth\u003eName\u003c/th\u003e\n      \u003cth\u003eSex\u003c/th\u003e\n      \u003cth\u003eAge\u003c/th\u003e\n      \u003cth\u003eSibSp\u003c/th\u003e\n      \u003cth\u003eParch\u003c/th\u003e\n      \u003cth\u003eTicket\u003c/th\u003e\n      \u003cth\u003eFare\u003c/th\u003e\n      \u003cth\u003eCabin\u003c/th\u003e\n      \u003cth\u003eEmbarked\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003cth\u003e0\u003c/th\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e0\u003c/td\u003e\n      \u003ctd\u003e3\u003c/td\u003e\n      \u003ctd\u003eBraund, Mr. Owen Harris\u003c/td\u003e\n      \u003ctd\u003emale\u003c/td\u003e\n      \u003ctd\u003e22.0\u003c/td\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e0\u003c/td\u003e\n      \u003ctd\u003eA/5 21171\u003c/td\u003e\n      \u003ctd\u003e7.2500\u003c/td\u003e\n      \u003ctd\u003eNaN\u003c/td\u003e\n      \u003ctd\u003eS\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e1\u003c/th\u003e\n      \u003ctd\u003e2\u003c/td\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003eCumings, Mrs. John Bradley (Florence Briggs Th...\u003c/td\u003e\n      \u003ctd\u003efemale\u003c/td\u003e\n      \u003ctd\u003e38.0\u003c/td\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e0\u003c/td\u003e\n      \u003ctd\u003ePC 17599\u003c/td\u003e\n      \u003ctd\u003e71.2833\u003c/td\u003e\n      \u003ctd\u003eC85\u003c/td\u003e\n      \u003ctd\u003eC\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e2\u003c/th\u003e\n      \u003ctd\u003e3\u003c/td\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e3\u003c/td\u003e\n      \u003ctd\u003eHeikkinen, Miss. Laina\u003c/td\u003e\n      \u003ctd\u003efemale\u003c/td\u003e\n      \u003ctd\u003e26.0\u003c/td\u003e\n      \u003ctd\u003e0\u003c/td\u003e\n      \u003ctd\u003e0\u003c/td\u003e\n      \u003ctd\u003eSTON/O2. 3101282\u003c/td\u003e\n      \u003ctd\u003e7.9250\u003c/td\u003e\n      \u003ctd\u003eNaN\u003c/td\u003e\n      \u003ctd\u003eS\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e3\u003c/th\u003e\n      \u003ctd\u003e4\u003c/td\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003eFutrelle, Mrs. Jacques Heath (Lily May Peel)\u003c/td\u003e\n      \u003ctd\u003efemale\u003c/td\u003e\n      \u003ctd\u003e35.0\u003c/td\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e0\u003c/td\u003e\n      \u003ctd\u003e113803\u003c/td\u003e\n      \u003ctd\u003e53.1000\u003c/td\u003e\n      \u003ctd\u003eC123\u003c/td\u003e\n      \u003ctd\u003eS\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e4\u003c/th\u003e\n      \u003ctd\u003e5\u003c/td\u003e\n      \u003ctd\u003e0\u003c/td\u003e\n      \u003ctd\u003e3\u003c/td\u003e\n      \u003ctd\u003eAllen, Mr. William Henry\u003c/td\u003e\n      \u003ctd\u003emale\u003c/td\u003e\n      \u003ctd\u003e35.0\u003c/td\u003e\n      \u003ctd\u003e0\u003c/td\u003e\n      \u003ctd\u003e0\u003c/td\u003e\n      \u003ctd\u003e373450\u003c/td\u003e\n      \u003ctd\u003e8.0500\u003c/td\u003e\n      \u003ctd\u003eNaN\u003c/td\u003e\n      \u003ctd\u003eS\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/div\u003e"
          },
          "metadata": {},
          "output_type": "execute_result",
          "execution_count": 102
        }
      ],
      "source": "import pandas as pd\nimport numpy as np\ntsdata \u003d pd.read_csv(\"titanic_train.csv\")  # 载入数据 泰坦尼克船员获获救预测数据集\ntsdata.head()\n# 数据即中的内容主要包括\n#passengerid:乘客id survived:是否幸存 pclass:舱位级别 name:名字 \n#sex:性别 age:年龄 sibsp:同亲数量 parch:父母数量 ticket:船票编码 \n#fare:船票价格 cabin:船舱位区 embarked:登船地点\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 数据查看",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 47,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "0     22.0\n1     38.0\n2     26.0\n3     35.0\n4     35.0\n5      NaN\n6     54.0\n7      2.0\n8     27.0\n9     14.0\n10     4.0\nName: Age, dtype: float64\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "age \u003d tsdata[\"Age\"]  # 前10记录中的年龄的大小 发现存在缺失值\nprint(age.loc[0:10])\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 缺失值处理",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 86,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "0      False\n1      False\n2      False\n3      False\n4      False\n5       True\n6      False\n7      False\n8      False\n9      False\n10     False\n11     False\n12     False\n13     False\n14     False\n15     False\n16     False\n17      True\n18     False\n19      True\n20     False\n21     False\n22     False\n23     False\n24     False\n25     False\n26      True\n27     False\n28      True\n29      True\n       ...  \n861    False\n862    False\n863     True\n864    False\n865    False\n866    False\n867    False\n868     True\n869    False\n870    False\n871    False\n872    False\n873    False\n874    False\n875    False\n876    False\n877    False\n878     True\n879    False\n880    False\n881    False\n882    False\n883    False\n884    False\n885    False\n886    False\n887    False\n888     True\n889    False\n890    False\nName: Age, Length: 891, dtype: bool\n5     NaN\n17    NaN\n19    NaN\n26    NaN\n28    NaN\n29    NaN\n31    NaN\n32    NaN\n36    NaN\n42    NaN\n45    NaN\n46    NaN\n47    NaN\n48    NaN\n55    NaN\n64    NaN\n65    NaN\n76    NaN\n77    NaN\n82    NaN\n87    NaN\n95    NaN\n101   NaN\n107   NaN\n109   NaN\n121   NaN\n126   NaN\n128   NaN\n140   NaN\n154   NaN\n       ..\n718   NaN\n727   NaN\n732   NaN\n738   NaN\n739   NaN\n740   NaN\n760   NaN\n766   NaN\n768   NaN\n773   NaN\n776   NaN\n778   NaN\n783   NaN\n790   NaN\n792   NaN\n793   NaN\n815   NaN\n825   NaN\n826   NaN\n828   NaN\n832   NaN\n837   NaN\n839   NaN\n846   NaN\n849   NaN\n859   NaN\n863   NaN\n868   NaN\n878   NaN\n888   NaN\nName: Age, Length: 177, dtype: float64\n177\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "age_is_null \u003d pd.isnull(age)  # 布尔判断 缺失值标记为true\n# age_is_rec \u003d pd.istrue(age) # 没有istrue 判断\nprint(age_is_null)\nage_null_true_data \u003d age[age_is_null]  # 使用返回的布尔判断作为索引,索引出缺失值的记录\nprint(age_null_true_data)\nage_null_true_data_count \u003d len(age_null_true_data)\nprint(age_null_true_data_count)  # 统计空记录值的记录条数\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 数据统计 (不处理缺失值的情况下)",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 64,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "nan\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "# 统计平均年龄\nmean_age \u003d sum(tsdata[\"Age\"]) / len(tsdata[\"Age\"]) # 不处理缺失值统计均年龄\nprint(mean_age)  # nan 无\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 数据统计 处理缺失值\n#### 可以通过 众数, 均值, 中位数 等统计数值对缺失值进行填充",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# 处理缺失值\n#print(age_is_null)\ngood_ages \u003d tsdata[\"Age\"][age_is_null \u003d\u003d False]  # 匹配出无缺失值的年龄\n#print(good_ages)\ncorrect_mean_age \u003d sum(good_ages) / len(good_ages)  # 无缺失值的均年龄统计\nprint(\"使用公式计算均值\",correct_mean_age)\n\ncorrect_mean_age_2 \u003d tsdata[\"Age\"].mean()\nprint(\"方法2函数直接计算均值\",correct_mean_age_2)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "使用 ,dropna .fillna 剔除 填充null值",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 120,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "原数据    PassengerId  Survived  Pclass  \\\n0            1         0       3   \n1            2         1       1   \n2            3         1       3   \n3            4         1       1   \n4            5         0       3   \n\n                                                Name     Sex   Age  SibSp  \\\n0                            Braund, Mr. Owen Harris    male  22.0      1   \n1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n2                             Heikkinen, Miss. Laina  female  26.0      0   \n3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n4                           Allen, Mr. William Henry    male  35.0      0   \n\n   Parch            Ticket     Fare Cabin Embarked  \n0      0         A/5 21171   7.2500   NaN        S  \n1      0          PC 17599  71.2833   C85        C  \n2      0  STON/O2. 3101282   7.9250   NaN        S  \n3      0            113803  53.1000  C123        S  \n4      0            373450   8.0500   NaN        S  \n剔除null了      PassengerId  Survived  Pclass  \\\n0              1         0       3   \n1              2         1       1   \n2              3         1       3   \n3              4         1       1   \n4              5         0       3   \n5              6         0       3   \n6              7         0       1   \n7              8         0       3   \n8              9         1       3   \n9             10         1       2   \n10            11         1       3   \n11            12         1       1   \n12            13         0       3   \n13            14         0       3   \n14            15         0       3   \n15            16         1       2   \n16            17         0       3   \n17            18         1       2   \n18            19         0       3   \n19            20         1       3   \n20            21         0       2   \n21            22         1       2   \n22            23         1       3   \n23            24         1       1   \n24            25         0       3   \n25            26         1       3   \n26            27         0       3   \n27            28         0       1   \n28            29         1       3   \n29            30         0       3   \n..           ...       ...     ...   \n861          862         0       2   \n862          863         1       1   \n863          864         0       3   \n864          865         0       2   \n865          866         1       2   \n866          867         1       2   \n867          868         0       1   \n868          869         0       3   \n869          870         1       3   \n870          871         0       3   \n871          872         1       1   \n872          873         0       1   \n873          874         0       3   \n874          875         1       2   \n875          876         1       3   \n876          877         0       3   \n877          878         0       3   \n878          879         0       3   \n879          880         1       1   \n880          881         1       2   \n881          882         0       3   \n882          883         0       3   \n883          884         0       2   \n884          885         0       3   \n885          886         0       3   \n886          887         0       2   \n887          888         1       1   \n888          889         0       3   \n889          890         1       1   \n890          891         0       3   \n\n                                                  Name     Sex  SibSp  Parch  \\\n0                              Braund, Mr. Owen Harris    male      1      0   \n1    Cumings, Mrs. John Bradley (Florence Briggs Th...  female      1      0   \n2                               Heikkinen, Miss. Laina  female      0      0   \n3         Futrelle, Mrs. Jacques Heath (Lily May Peel)  female      1      0   \n4                             Allen, Mr. William Henry    male      0      0   \n5                                     Moran, Mr. James    male      0      0   \n6                              McCarthy, Mr. Timothy J    male      0      0   \n7                       Palsson, Master. Gosta Leonard    male      3      1   \n8    Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)  female      0      2   \n9                  Nasser, Mrs. Nicholas (Adele Achem)  female      1      0   \n10                     Sandstrom, Miss. Marguerite Rut  female      1      1   \n11                            Bonnell, Miss. Elizabeth  female      0      0   \n12                      Saundercock, Mr. William Henry    male      0      0   \n13                         Andersson, Mr. Anders Johan    male      1      5   \n14                Vestrom, Miss. Hulda Amanda Adolfina  female      0      0   \n15                    Hewlett, Mrs. (Mary D Kingcome)   female      0      0   \n16                                Rice, Master. Eugene    male      4      1   \n17                        Williams, Mr. Charles Eugene    male      0      0   \n18   Vander Planke, Mrs. Julius (Emelia Maria Vande...  female      1      0   \n19                             Masselmani, Mrs. Fatima  female      0      0   \n20                                Fynney, Mr. Joseph J    male      0      0   \n21                               Beesley, Mr. Lawrence    male      0      0   \n22                         McGowan, Miss. Anna \"Annie\"  female      0      0   \n23                        Sloper, Mr. William Thompson    male      0      0   \n24                       Palsson, Miss. Torborg Danira  female      3      1   \n25   Asplund, Mrs. Carl Oscar (Selma Augusta Emilia...  female      1      5   \n26                             Emir, Mr. Farred Chehab    male      0      0   \n27                      Fortune, Mr. Charles Alexander    male      3      2   \n28                       O\u0027Dwyer, Miss. Ellen \"Nellie\"  female      0      0   \n29                                 Todoroff, Mr. Lalio    male      0      0   \n..                                                 ...     ...    ...    ...   \n861                        Giles, Mr. Frederick Edward    male      1      0   \n862  Swift, Mrs. Frederick Joel (Margaret Welles Ba...  female      0      0   \n863                  Sage, Miss. Dorothy Edith \"Dolly\"  female      8      2   \n864                             Gill, Mr. John William    male      0      0   \n865                           Bystrom, Mrs. (Karolina)  female      0      0   \n866                       Duran y More, Miss. Asuncion  female      1      0   \n867               Roebling, Mr. Washington Augustus II    male      0      0   \n868                        van Melkebeke, Mr. Philemon    male      0      0   \n869                    Johnson, Master. Harold Theodor    male      1      1   \n870                                  Balkic, Mr. Cerin    male      0      0   \n871   Beckwith, Mrs. Richard Leonard (Sallie Monypeny)  female      1      1   \n872                           Carlsson, Mr. Frans Olof    male      0      0   \n873                        Vander Cruyssen, Mr. Victor    male      0      0   \n874              Abelson, Mrs. Samuel (Hannah Wizosky)  female      1      0   \n875                   Najib, Miss. Adele Kiamie \"Jane\"  female      0      0   \n876                      Gustafsson, Mr. Alfred Ossian    male      0      0   \n877                               Petroff, Mr. Nedelio    male      0      0   \n878                                 Laleff, Mr. Kristo    male      0      0   \n879      Potter, Mrs. Thomas Jr (Lily Alexenia Wilson)  female      0      1   \n880       Shelley, Mrs. William (Imanita Parrish Hall)  female      0      1   \n881                                 Markun, Mr. Johann    male      0      0   \n882                       Dahlberg, Miss. Gerda Ulrika  female      0      0   \n883                      Banfield, Mr. Frederick James    male      0      0   \n884                             Sutehall, Mr. Henry Jr    male      0      0   \n885               Rice, Mrs. William (Margaret Norton)  female      0      5   \n886                              Montvila, Rev. Juozas    male      0      0   \n887                       Graham, Miss. Margaret Edith  female      0      0   \n888           Johnston, Miss. Catherine Helen \"Carrie\"  female      1      2   \n889                              Behr, Mr. Karl Howell    male      0      0   \n890                                Dooley, Mr. Patrick    male      0      0   \n\n               Ticket      Fare  \n0           A/5 21171    7.2500  \n1            PC 17599   71.2833  \n2    STON/O2. 3101282    7.9250  \n3              113803   53.1000  \n4              373450    8.0500  \n5              330877    8.4583  \n6               17463   51.8625  \n7              349909   21.0750  \n8              347742   11.1333  \n9              237736   30.0708  \n10            PP 9549   16.7000  \n11             113783   26.5500  \n12          A/5. 2151    8.0500  \n13             347082   31.2750  \n14             350406    7.8542  \n15             248706   16.0000  \n16             382652   29.1250  \n17             244373   13.0000  \n18             345763   18.0000  \n19               2649    7.2250  \n20             239865   26.0000  \n21             248698   13.0000  \n22             330923    8.0292  \n23             113788   35.5000  \n24             349909   21.0750  \n25             347077   31.3875  \n26               2631    7.2250  \n27              19950  263.0000  \n28             330959    7.8792  \n29             349216    7.8958  \n..                ...       ...  \n861             28134   11.5000  \n862             17466   25.9292  \n863          CA. 2343   69.5500  \n864            233866   13.0000  \n865            236852   13.0000  \n866     SC/PARIS 2149   13.8583  \n867          PC 17590   50.4958  \n868            345777    9.5000  \n869            347742   11.1333  \n870            349248    7.8958  \n871             11751   52.5542  \n872               695    5.0000  \n873            345765    9.0000  \n874         P/PP 3381   24.0000  \n875              2667    7.2250  \n876              7534    9.8458  \n877            349212    7.8958  \n878            349217    7.8958  \n879             11767   83.1583  \n880            230433   26.0000  \n881            349257    7.8958  \n882              7552   10.5167  \n883  C.A./SOTON 34068   10.5000  \n884   SOTON/OQ 392076    7.0500  \n885            382652   29.1250  \n886            211536   13.0000  \n887            112053   30.0000  \n888        W./C. 6607   23.4500  \n889            111369   30.0000  \n890            370376    7.7500  \n\n[891 rows x 9 columns]\n     PassengerId  Survived  Pclass  \\\n0              1         0       3   \n1              2         1       1   \n2              3         1       3   \n3              4         1       1   \n4              5         0       3   \n6              7         0       1   \n7              8         0       3   \n8              9         1       3   \n9             10         1       2   \n10            11         1       3   \n11            12         1       1   \n12            13         0       3   \n13            14         0       3   \n14            15         0       3   \n15            16         1       2   \n16            17         0       3   \n18            19         0       3   \n20            21         0       2   \n21            22         1       2   \n22            23         1       3   \n23            24         1       1   \n24            25         0       3   \n25            26         1       3   \n27            28         0       1   \n30            31         0       1   \n33            34         0       2   \n34            35         0       1   \n35            36         0       1   \n37            38         0       3   \n38            39         0       3   \n..           ...       ...     ...   \n856          857         1       1   \n857          858         1       1   \n858          859         1       3   \n860          861         0       3   \n861          862         0       2   \n862          863         1       1   \n864          865         0       2   \n865          866         1       2   \n866          867         1       2   \n867          868         0       1   \n869          870         1       3   \n870          871         0       3   \n871          872         1       1   \n872          873         0       1   \n873          874         0       3   \n874          875         1       2   \n875          876         1       3   \n876          877         0       3   \n877          878         0       3   \n879          880         1       1   \n880          881         1       2   \n881          882         0       3   \n882          883         0       3   \n883          884         0       2   \n884          885         0       3   \n885          886         0       3   \n886          887         0       2   \n887          888         1       1   \n889          890         1       1   \n890          891         0       3   \n\n                                                  Name     Sex   Age  SibSp  \\\n0                              Braund, Mr. Owen Harris    male  22.0      1   \n1    Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n2                               Heikkinen, Miss. Laina  female  26.0      0   \n3         Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n4                             Allen, Mr. William Henry    male  35.0      0   \n6                              McCarthy, Mr. Timothy J    male  54.0      0   \n7                       Palsson, Master. Gosta Leonard    male   2.0      3   \n8    Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)  female  27.0      0   \n9                  Nasser, Mrs. Nicholas (Adele Achem)  female  14.0      1   \n10                     Sandstrom, Miss. Marguerite Rut  female   4.0      1   \n11                            Bonnell, Miss. Elizabeth  female  58.0      0   \n12                      Saundercock, Mr. William Henry    male  20.0      0   \n13                         Andersson, Mr. Anders Johan    male  39.0      1   \n14                Vestrom, Miss. Hulda Amanda Adolfina  female  14.0      0   \n15                    Hewlett, Mrs. (Mary D Kingcome)   female  55.0      0   \n16                                Rice, Master. Eugene    male   2.0      4   \n18   Vander Planke, Mrs. Julius (Emelia Maria Vande...  female  31.0      1   \n20                                Fynney, Mr. Joseph J    male  35.0      0   \n21                               Beesley, Mr. Lawrence    male  34.0      0   \n22                         McGowan, Miss. Anna \"Annie\"  female  15.0      0   \n23                        Sloper, Mr. William Thompson    male  28.0      0   \n24                       Palsson, Miss. Torborg Danira  female   8.0      3   \n25   Asplund, Mrs. Carl Oscar (Selma Augusta Emilia...  female  38.0      1   \n27                      Fortune, Mr. Charles Alexander    male  19.0      3   \n30                            Uruchurtu, Don. Manuel E    male  40.0      0   \n33                               Wheadon, Mr. Edward H    male  66.0      0   \n34                             Meyer, Mr. Edgar Joseph    male  28.0      1   \n35                      Holverson, Mr. Alexander Oskar    male  42.0      1   \n37                            Cann, Mr. Ernest Charles    male  21.0      0   \n38                  Vander Planke, Miss. Augusta Maria  female  18.0      2   \n..                                                 ...     ...   ...    ...   \n856         Wick, Mrs. George Dennick (Mary Hitchcock)  female  45.0      1   \n857                             Daly, Mr. Peter Denis     male  51.0      0   \n858              Baclini, Mrs. Solomon (Latifa Qurban)  female  24.0      0   \n860                            Hansen, Mr. Claus Peter    male  41.0      2   \n861                        Giles, Mr. Frederick Edward    male  21.0      1   \n862  Swift, Mrs. Frederick Joel (Margaret Welles Ba...  female  48.0      0   \n864                             Gill, Mr. John William    male  24.0      0   \n865                           Bystrom, Mrs. (Karolina)  female  42.0      0   \n866                       Duran y More, Miss. Asuncion  female  27.0      1   \n867               Roebling, Mr. Washington Augustus II    male  31.0      0   \n869                    Johnson, Master. Harold Theodor    male   4.0      1   \n870                                  Balkic, Mr. Cerin    male  26.0      0   \n871   Beckwith, Mrs. Richard Leonard (Sallie Monypeny)  female  47.0      1   \n872                           Carlsson, Mr. Frans Olof    male  33.0      0   \n873                        Vander Cruyssen, Mr. Victor    male  47.0      0   \n874              Abelson, Mrs. Samuel (Hannah Wizosky)  female  28.0      1   \n875                   Najib, Miss. Adele Kiamie \"Jane\"  female  15.0      0   \n876                      Gustafsson, Mr. Alfred Ossian    male  20.0      0   \n877                               Petroff, Mr. Nedelio    male  19.0      0   \n879      Potter, Mrs. Thomas Jr (Lily Alexenia Wilson)  female  56.0      0   \n880       Shelley, Mrs. William (Imanita Parrish Hall)  female  25.0      0   \n881                                 Markun, Mr. Johann    male  33.0      0   \n882                       Dahlberg, Miss. Gerda Ulrika  female  22.0      0   \n883                      Banfield, Mr. Frederick James    male  28.0      0   \n884                             Sutehall, Mr. Henry Jr    male  25.0      0   \n885               Rice, Mrs. William (Margaret Norton)  female  39.0      0   \n886                              Montvila, Rev. Juozas    male  27.0      0   \n887                       Graham, Miss. Margaret Edith  female  19.0      0   \n889                              Behr, Mr. Karl Howell    male  26.0      0   \n890                                Dooley, Mr. Patrick    male  32.0      0   \n\n     Parch            Ticket      Fare        Cabin Embarked  \n0        0         A/5 21171    7.2500          NaN        S  \n1        0          PC 17599   71.2833          C85        C  \n2        0  STON/O2. 3101282    7.9250          NaN        S  \n3        0            113803   53.1000         C123        S  \n4        0            373450    8.0500          NaN        S  \n6        0             17463   51.8625          E46        S  \n7        1            349909   21.0750          NaN        S  \n8        2            347742   11.1333          NaN        S  \n9        0            237736   30.0708          NaN        C  \n10       1           PP 9549   16.7000           G6        S  \n11       0            113783   26.5500         C103        S  \n12       0         A/5. 2151    8.0500          NaN        S  \n13       5            347082   31.2750          NaN        S  \n14       0            350406    7.8542          NaN        S  \n15       0            248706   16.0000          NaN        S  \n16       1            382652   29.1250          NaN        Q  \n18       0            345763   18.0000          NaN        S  \n20       0            239865   26.0000          NaN        S  \n21       0            248698   13.0000          D56        S  \n22       0            330923    8.0292          NaN        Q  \n23       0            113788   35.5000           A6        S  \n24       1            349909   21.0750          NaN        S  \n25       5            347077   31.3875          NaN        S  \n27       2             19950  263.0000  C23 C25 C27        S  \n30       0          PC 17601   27.7208          NaN        C  \n33       0        C.A. 24579   10.5000          NaN        S  \n34       0          PC 17604   82.1708          NaN        C  \n35       0            113789   52.0000          NaN        S  \n37       0        A./5. 2152    8.0500          NaN        S  \n38       0            345764   18.0000          NaN        S  \n..     ...               ...       ...          ...      ...  \n856      1             36928  164.8667          NaN        S  \n857      0            113055   26.5500          E17        S  \n858      3              2666   19.2583          NaN        C  \n860      0            350026   14.1083          NaN        S  \n861      0             28134   11.5000          NaN        S  \n862      0             17466   25.9292          D17        S  \n864      0            233866   13.0000          NaN        S  \n865      0            236852   13.0000          NaN        S  \n866      0     SC/PARIS 2149   13.8583          NaN        C  \n867      0          PC 17590   50.4958          A24        S  \n869      1            347742   11.1333          NaN        S  \n870      0            349248    7.8958          NaN        S  \n871      1             11751   52.5542          D35        S  \n872      0               695    5.0000  B51 B53 B55        S  \n873      0            345765    9.0000          NaN        S  \n874      0         P/PP 3381   24.0000          NaN        C  \n875      0              2667    7.2250          NaN        C  \n876      0              7534    9.8458          NaN        S  \n877      0            349212    7.8958          NaN        S  \n879      1             11767   83.1583          C50        C  \n880      1            230433   26.0000          NaN        S  \n881      0            349257    7.8958          NaN        S  \n882      0              7552   10.5167          NaN        S  \n883      0  C.A./SOTON 34068   10.5000          NaN        S  \n884      0   SOTON/OQ 392076    7.0500          NaN        S  \n885      5            382652   29.1250          NaN        Q  \n886      0            211536   13.0000          NaN        S  \n887      0            112053   30.0000          B42        S  \n889      0            111369   30.0000         C148        C  \n890      0            370376    7.7500          NaN        Q  \n\n[714 rows x 12 columns]\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "print(\"原数据\",tsdata.head())\ndrop_na_columns \u003d tsdata.dropna(axis\u003d1) # 参数 axis:1 行记录的null剔除\nprint(\"剔除null了\", drop_na_columns)\n\ndrop_na_row \u003d tsdata.dropna(axis\u003d0, subset\u003d[\"Age\", \"Sex\"])  # axis:0 指定属性列\nprint(drop_na_row)\n\n# 使用fillna 填充null\n# fill_na_cols \u003d tsdata.fillna()",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 手动计算每个等级的仓位的均价",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "source": "# 每个等级的仓位的均价\npassenger_class \u003d [1,2,3]  # 构建等级索引列表,用于不同等级的记录的匹配\nfares_by_class \u003d {}  # 存储结果的字典\nprint(type(fares_by_class))\nfor this_class in passenger_class: # 对每个等级的记录进行遍历求均价\n    pclass_rows \u003d tsdata[tsdata[\"Pclass\"] \u003d\u003d this_class]  # 筛选出该等级的记录\n    pclass_fares \u003d pclass_rows[\"Fare\"] #提取该等级的仓位价格\n    fare_for_class \u003d pclass_fares.mean()  # 计算该等级的仓位价格均值\n    fares_by_class[this_class] \u003d fare_for_class # 计算的均值存放在结果的列表中\n    print(fare_for_class)\nprint(fares_by_class)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      },
      "execution_count": 99,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "\u003cclass \u0027dict\u0027\u003e\n84.15468749999992\n20.66218315217391\n13.675550101832997\n{1: 84.15468749999992, 2: 20.66218315217391, 3: 13.675550101832997}\n"
          ],
          "output_type": "stream"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": ".pivot_table 计算数据透视表\n.pivot(max, 统计变量, 统计方式)\n.pivot(index\u003d索引属性, 统计变量, 统计方式)",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 108,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "        Survived\nPclass          \n1       0.629630\n2       0.472826\n3       0.242363\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "# 使用 v.pivot_table 统计不同等级的仓位的幸存的概率\npassenger_survival_proba \u003d tsdata.pivot_table(index\u003d\"Pclass\", values\u003d \"Survived\", aggfunc\u003dnp.mean)\n# 参数: \nprint(passenger_survival_proba)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 109,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "              Age\nPclass           \n1       38.233441\n2       29.877630\n3       25.140620\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "# 使用 v.pivot_table 统计不同等级的仓位平均年龄 默认的统计aggfunc 是求均值\npassenger_class_age_mean \u003d tsdata.pivot_table(index\u003d\"Pclass\", values\u003d\"Age\")\nprint(passenger_class_age_mean) \n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 使用pivot_table 进行多个变量属性的数据统计",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 110,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "港口状态               Fare  Survived\nPclass                      \n1       18177.4125       136\n2        3801.8417        87\n3        6714.6951       119\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "# 统计登船港口的状态\nport_stats \u003d tsdata.pivot_table(index\u003d\"Pclass\", values\u003d[\"Fare\", \"Survived\"], aggfunc\u003dnp.sum)\nprint(\"港口状态\", port_stats)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 对矩阵中的数据进行排序",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 127,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "     PassengerId  Survived  Pclass                                  Name  \\\n630          631         1       1  Barkworth, Mr. Algernon Henry Wilson   \n851          852         0       3                   Svensson, Mr. Johan   \n493          494         0       1               Artagaveytia, Mr. Ramon   \n96            97         0       1             Goldschmidt, Mr. George B   \n116          117         0       3                  Connors, Mr. Patrick   \n672          673         0       2           Mitchell, Mr. Henry Michael   \n745          746         0       1          Crosby, Capt. Edward Gifford   \n33            34         0       2                 Wheadon, Mr. Edward H   \n54            55         0       1        Ostby, Mr. Engelhart Cornelius   \n280          281         0       3                      Duane, Mr. Frank   \n\n      Sex   Age  SibSp  Parch      Ticket     Fare Cabin Embarked  \n630  male  80.0      0      0       27042  30.0000   A23        S  \n851  male  74.0      0      0      347060   7.7750   NaN        S  \n493  male  71.0      0      0    PC 17609  49.5042   NaN        C  \n96   male  71.0      0      0    PC 17754  34.6542    A5        C  \n116  male  70.5      0      0      370369   7.7500   NaN        Q  \n672  male  70.0      0      0  C.A. 24580  10.5000   NaN        S  \n745  male  70.0      1      1   WE/P 5735  71.0000   B22        S  \n33   male  66.0      0      0  C.A. 24579  10.5000   NaN        S  \n54   male  65.0      0      1      113509  61.9792   B30        C  \n280  male  65.0      0      0      336439   7.7500   NaN        Q  \n   PassengerId  Survived  Pclass                                  Name   Sex  \\\n0          631         1       1  Barkworth, Mr. Algernon Henry Wilson  male   \n1          852         0       3                   Svensson, Mr. Johan  male   \n2          494         0       1               Artagaveytia, Mr. Ramon  male   \n3           97         0       1             Goldschmidt, Mr. George B  male   \n4          117         0       3                  Connors, Mr. Patrick  male   \n5          673         0       2           Mitchell, Mr. Henry Michael  male   \n6          746         0       1          Crosby, Capt. Edward Gifford  male   \n7           34         0       2                 Wheadon, Mr. Edward H  male   \n8           55         0       1        Ostby, Mr. Engelhart Cornelius  male   \n9          281         0       3                      Duane, Mr. Frank  male   \n\n    Age  SibSp  Parch      Ticket     Fare Cabin Embarked  \n0  80.0      0      0       27042  30.0000   A23        S  \n1  74.0      0      0      347060   7.7750   NaN        S  \n2  71.0      0      0    PC 17609  49.5042   NaN        C  \n3  71.0      0      0    PC 17754  34.6542    A5        C  \n4  70.5      0      0      370369   7.7500   NaN        Q  \n5  70.0      0      0  C.A. 24580  10.5000   NaN        S  \n6  70.0      1      1   WE/P 5735  71.0000   B22        S  \n7  66.0      0      0  C.A. 24579  10.5000   NaN        S  \n8  65.0      0      1      113509  61.9792   B30        C  \n9  65.0      0      0      336439   7.7500   NaN        Q  \n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "new_ts_survival \u003d tsdata.sort_values(\"Age\", ascending\u003dFalse) # age降序排序\nprint(new_ts_survival[0:10])  \nnew_ts_index \u003dnew_ts_survival.reset_index(drop\u003dTrue)  # 对age排序后的矩阵,再次进行索引排序, drop:True 重新排序\nprint(new_ts_index[0:10])\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 自定义函数进行数据整合统计 将不同的统计函数进行组合\n### 执行自定义的函数使用 .apply(自定义函数名) ",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 128,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "PassengerId                  100\nSurvived                       0\nPclass                         2\nName           Kantor, Mr. Sinai\nSex                         male\nAge                           34\nSibSp                          1\nParch                          0\nTicket                    244367\nFare                          26\nCabin                        NaN\nEmbarked                       S\ndtype: object\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "def hundreth_row(column):  # 第一百行数据\n    hundreth_item \u003d column.loc[99] # 取99行数据\n    return hundreth_item\n\nhundreth_row \u003d tsdata.apply(hundreth_row)  # 更新获取的行数据\nprint(hundreth_row) \n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 统计属性null行数",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 132,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "PassengerId      0\nSurvived         0\nPclass           0\nName             0\nSex              0\nAge            177\nSibSp            0\nParch            0\nTicket           0\nFare             0\nCabin          687\nEmbarked         2\ndtype: int64\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "def not_null_count(column):\n    column_null \u003d pd.isnull(column)\n    null \u003d column[column_null]\n    return len(null)\ncolumn_null_count \u003d tsdata.apply(not_null_count)\nprint(column_null_count)\n\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 136,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "0      False\n1      False\n2      False\n3      False\n4      False\n5       True\n6      False\n7      False\n8      False\n9      False\n10     False\n11     False\n12     False\n13     False\n14     False\n15     False\n16     False\n17      True\n18     False\n19      True\n20     False\n21     False\n22     False\n23     False\n24     False\n25     False\n26      True\n27     False\n28      True\n29      True\n       ...  \n861    False\n862    False\n863     True\n864    False\n865    False\n866    False\n867    False\n868     True\n869    False\n870    False\n871    False\n872    False\n873    False\n874    False\n875    False\n876    False\n877    False\n878     True\n879    False\n880    False\n881    False\n882    False\n883    False\n884    False\n885    False\n886    False\n887    False\n888     True\n889    False\n890    False\nName: Age, Length: 891, dtype: bool\n"
          ],
          "output_type": "stream"
        },
        {
          "traceback": [
            "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[1;32m\u003cipython-input-136-b35c51514b19\u003e\u001b[0m in \u001b[0;36m\u003cmodule\u003e\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mnull\u001b[0m \u001b[1;33m\u003d\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misnull\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtsdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"Age\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----\u003e 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtsdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misnull\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtsdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"Age\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36mapply\u001b[1;34m(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)\u001b[0m\n\u001b[0;32m   6002\u001b[0m                          \u001b[0margs\u001b[0m\u001b[1;33m\u003d\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6003\u001b[0m                          kwds\u003dkwds)\n\u001b[1;32m-\u003e 6004\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mop\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_result\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   6005\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6006\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mapplymap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
            "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\apply.py\u001b[0m in \u001b[0;36mget_result\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    316\u001b[0m                                       *self.args, **self.kwds)\n\u001b[0;32m    317\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--\u003e 318\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mFrameRowApply\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_result\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    319\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    320\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mapply_broadcast\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
            "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\apply.py\u001b[0m in \u001b[0;36mget_result\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    140\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_raw\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    141\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--\u003e 142\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_standard\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    143\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    144\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mapply_empty_result\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
            "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\apply.py\u001b[0m in \u001b[0;36mapply_standard\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    246\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    247\u001b[0m         \u001b[1;31m# compute the result using the series generator\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--\u003e 248\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_series_generator\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    249\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    250\u001b[0m         \u001b[1;31m# wrap results\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
            "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\apply.py\u001b[0m in \u001b[0;36mapply_series_generator\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    275\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    276\u001b[0m                 \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mv\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mseries_gen\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--\u003e 277\u001b[1;33m                     \u001b[0mresults\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m\u003d\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    278\u001b[0m                     \u001b[0mkeys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    279\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
            "\u001b[1;31mTypeError\u001b[0m: (\"\u0027Series\u0027 object is not callable\", \u0027occurred at index PassengerId\u0027)"
          ],
          "ename": "TypeError",
          "evalue": "(\"\u0027Series\u0027 object is not callable\", \u0027occurred at index PassengerId\u0027)",
          "output_type": "error"
        }
      ],
      "source": "null \u003d pd.isnull(tsdata[\"Age\"])\nprint(null)",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "对数据处理结果进行矩阵内的属性的显示",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 141,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "0       adult\n1       adult\n2       adult\n3       adult\n4       adult\n5      unknow\n6       adult\n7       minor\n8       adult\n9       minor\n10      minor\n11      adult\n12      adult\n13      adult\n14      minor\n15      adult\n16      minor\n17     unknow\n18      adult\n19     unknow\n20      adult\n21      adult\n22      minor\n23      adult\n24      minor\n25      adult\n26     unknow\n27      adult\n28     unknow\n29     unknow\n        ...  \n861     adult\n862     adult\n863    unknow\n864     adult\n865     adult\n866     adult\n867     adult\n868    unknow\n869     minor\n870     adult\n871     adult\n872     adult\n873     adult\n874     adult\n875     minor\n876     adult\n877     adult\n878    unknow\n879     adult\n880     adult\n881     adult\n882     adult\n883     adult\n884     adult\n885     adult\n886     adult\n887     adult\n888    unknow\n889     adult\n890     adult\nLength: 891, dtype: object\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "# 对年龄进行判断 划归区间\ndef generate_age_lable(row):  # row 实际上代表矩阵\n    age \u003d row[\"Age\"]\n    if pd.isnull(age):\n        return \"unknow\"\n    elif age \u003c 18:\n        return \"minor\"\n    else:return \"adult\"\nage_lables \u003d tsdata.apply(generate_age_lable, axis\u003d1)\nprint(age_lables)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 对年龄的标签分类进行幸存率的统计",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 142,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "            Survived\nage_lables          \nadult       0.381032\nminor       0.539823\nunknow      0.293785\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "tsdata[\"age_lables\"] \u003d age_lables # 将新分类的年龄的标签加入到矩阵的记录属性中\nage_group_survived \u003d tsdata.pivot_table(index\u003d\"age_lables\", values\u003d\"Survived\", aggfunc\u003dnp.mean)\nprint(age_group_survived)",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    }
  ],
  "metadata": {
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 2
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython2",
      "version": "2.7.6"
    },
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}